//
//  EditUserInfoController.m
//  Otaku
//
//  Created by lanou3g on 16/5/13.
//  Copyright © 2016年 Liuwx. All rights reserved.
//

#import "EditUserInfoController.h"

@interface EditUserInfoController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *HeaderImageView;

@property (weak, nonatomic) IBOutlet UITextField *nameTextField;

@property (weak, nonatomic) IBOutlet UITextView *SignatureTextView;

@end

@implementation EditUserInfoController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupBasic];
    // Do any additional setup after loading the view from its nib.
}

- (void)setupBasic {
    
    self.title = @"个人信息";
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"headerImage"];
    self.HeaderImageView.image = [UIImage imageWithContentsOfFile:path];
    
    self.HeaderImageView.userInteractionEnabled = YES;
    
    [self.HeaderImageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changeHeaderImage)]];
    
    self.HeaderImageView.layer.cornerRadius = self.HeaderImageView.frame.size.width *0.5;
    self.HeaderImageView.layer.masksToBounds = YES;
    
    self.nameTextField.text = [[NSUserDefaults standardUserDefaults] stringForKey:UserNameKey];
    self.SignatureTextView.text = [[NSUserDefaults standardUserDefaults] stringForKey:UserSignatureKey];
    self.SignatureTextView.layer.cornerRadius = 5;
    self.SignatureTextView.layer.masksToBounds = YES;
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateSkinModel) name:SkinModelDidChangedNotification object:nil];
    [self updateSkinModel];
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [[NSUserDefaults standardUserDefaults] setObject:self.nameTextField.text forKey:UserNameKey];
    [[NSUserDefaults standardUserDefaults] setObject:self.SignatureTextView.text forKey:UserSignatureKey];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    
}

- (void)updateSkinModel {
    
    NSString *currentSkinModel = [[NSUserDefaults standardUserDefaults] stringForKey:CurrentSkinModelKey];
    
    if ([currentSkinModel isEqualToString:NightSkinModelValue]) {
        self.view.backgroundColor = [UIColor blackColor];
        self.nameTextField.backgroundColor = [UIColor darkGrayColor];
        self.SignatureTextView.backgroundColor = [UIColor darkGrayColor];
        self.nameTextField.textColor  = [UIColor grayColor];
        self.SignatureTextView.textColor = [UIColor grayColor];
        
    }else {
        self.view.backgroundColor = [UIColor colorWithRed:250.0/255.0 green:250.0/255.0  blue:250.0/255.0  alpha:1.0];
        self.nameTextField.backgroundColor = [UIColor whiteColor];
        self.SignatureTextView.backgroundColor = [UIColor whiteColor];
        self.nameTextField.textColor = [UIColor blackColor];
        self.SignatureTextView.textColor = [UIColor blackColor];
        
    }
    
}


- (void)changeHeaderImage {
    UIImagePickerController *controller = [[UIImagePickerController alloc]init];
    controller.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    controller.allowsEditing = YES;
    controller.delegate = self;
    [self presentViewController:controller animated:YES completion:nil];
    

}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo {
    self.HeaderImageView.image = image;
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"headerImage"];
    
    [UIImagePNGRepresentation(self.HeaderImageView.image) writeToFile:path atomically:YES];
    [self dismissViewControllerAnimated:self completion:nil];
    
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
